<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create confirm-form to send order and
 * overview order-details
 * get: confirm.tpl.php
 * used from cart.order.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */

if (isset($_SESSION['ordererIdx']))
{ //Create data for order if adress ok
	$pagefile = 'module/function/price.calculate.php';
	if (is_file($pagefile))
		include_once($pagefile);

	$refPage->assign('titel', _('Order'));

// Orderadress 
	$refPage->assign('orderadress', _('Orderadress'));
	db::getInstance()->where('orderer_id', $_SESSION['ordererIdx']);
	$result = db::getInstance()->getOne('orderer');

	$refPage->assign('company', $result['orderer_company']);
	$refPage->assign('name', $result['orderer_firstname'] . ' ' . $result['orderer_lastname']);
	$refPage->assign('street', $result['orderer_street']);
	$refPage->assign('city', $result['orderer_zip'] . ' ' . $result['orderer_city']);

	$refPage->assign('confirmlogo', '<img src="/template/images/logoemail.jpg">');
	$refPage->assign('companyname', COMPANY_NAME);
	$refPage->assign('companystreet', COMPANY_STREET);
	$refPage->assign('companycity', COMPANY_CITY);
	$refPage->assign('companyphone', COMPANY_PHONE);
	$refPage->assign('companyfax', COMPANY_FAX);
	$refPage->assign('companyemail', COMPANY_EMAIL);

	db::getInstance()->where('config_key', 'ordernumber');
	$result = db::getInstance()->getOne('config');
	$ordernumber = $result['config_value'];
	//$result = NULL;

	$refPage->assign('ordernumber', intval($ordernumber));
// productlist
	$cart = $refCart->getProducts();
	$price = '';
	$tax = array();
	$cartx = array();
	$pricex = 0;

	$refCart->addToTotalPrice(0, TRUE);

	foreach ($cart as $product)
	{
		$pricePerItem = $refCart->productPricePerItem($product['cart_product_id'],
			$product['number'], $product);
		$priceTotal = $refCart->productPrice($product['cart_product_id'], $product['number'],
			$product);
		$refCart->addToTotalPrice($priceTotal, $product['cart_product_id']);

		$tax = $refCart->getTax($priceTotal, $product['cart_product_id']);

		$orderx[] = array(
		 'sku' => $product['sku'],
		 'product' => $product['product'] .
		 $refCart->optionsText2Html($product['optionsText']),
		 'productId' => $product['cart_product_id'],
		 'pricePerItem' => $pricePerItem,
		 'priceTotal' => $priceTotal,
		 'tax' => $refCart->getTax($priceTotal, $product['cart_product_id'], FALSE),
		 'attribute' => 'attribute gets here',
		 'number' => $product['number']);
		//TODO include produkt-details
	}
	$refPage->assign('productList', $orderx);
	$_SESSION['productListx'] = $orderx;

	// shipping
	if (!isset($_SESSION['ordershippingx']) or isset($_POST['ordershipping']))
		$_SESSION['ordershippingx'] = $refValidate->cleanslash($_POST['ordershipping']);

	db::getInstance()->where('shipping_name', $_SESSION['ordershippingx']);
	$shipping = db::getInstance()->getOne('shipping');

	$_SESSION['shippingidx'] = $shipping['shipping_id'];
	$pagefile = 'plugins/shipping/' . $shipping['shipping_file'] . '.shipping.php';
	$sFunction = $shipping['shipping_file'];
	$sComment = $shipping['shipping_file'] . '_comment';
	//var_dump($shipping);exit;
	if (is_file($pagefile))
		include_once($pagefile);
	$addCost = $sFunction($cart, $shipping['shipping_value'], $shipping['shipping_table']);
	$refPage->assign('shippingPrice', $refCart->formatToPrice($addCost));
	$_SESSION['shippingtotalx'] = $refCart->formatToPrice($addCost);
	$_SESSION['shippingcommentx'] = $sComment();
	$refPage->assign('shippingComment', $_SESSION['shippingcommentx']);

	// prices
	$refCart->addSpecialToTotalPrice($addCost, $shipping['shipping_tax_id']);
	$refPage->assign('price', $refCart->getTotalPrice(TRUE));
	$_SESSION['totalpricex'] = $refCart->getTotalPrice(TRUE);
	$_SESSION['orderpricex'] = str_replace(',', '.', $refCart->getTotalPrice(FALSE));
	$refPage->assign('tax', $refCart->getTotalPriceTax(TRUE));
	$_SESSION['ordertaxx'] = $refCart->getTotalPriceTax(FALSE);

	// Comment
	if (!isset($_SESSION['commentx']) or isset($_POST['ordercommentx']))
		$_SESSION['commentx'] = $refValidate->cleanslash($_POST['ordercommentx']);
	if (!empty($_SESSION['commentx']))
		$refPage->assign('comment', $_SESSION['commentx']);

	// Payment
	if (!isset($_SESSION['paymentx']) or isset($_POST['orderpayment']))
		$_SESSION['paymentx'] = $refValidate->cleanslash($_POST['orderpayment']);

	db::getInstance()->where('payment_name', $_SESSION['paymentx']);
	$payment = db::getInstance()->getOne('payment');

	$_SESSION['paymentidx'] = $payment['payment_id'];
	$pagefile = 'plugins/payment/' . $payment['payment_file'] . '.payment.php';
	//$sFunction = $payment->payment_file;
	$sFunctionComment = $payment['payment_file'] . '_comment';

	if (is_file($pagefile))
		include_once($pagefile);

	$buffer = $sFunctionComment($payment['payment_id']);
	$refPage->assign('payment', $buffer);

	// Footer
	db::getInstance()->where('page_name', 'mail-footer');
	$buffer = db::getInstance()->getOne('page');
	$refPage->assign('footer', $buffer['page_text']);

	// Terms and revocation
	$refPage->assign('termsLink',
		'<a title="' . _('Terms') .
		'" href="/index.php?page=page&title=page-terms&name=' . _('Terms') . '" target="_blank">' .
		_('Link to the Terms in full text') . '</a>');
	$refPage->assign('revocationLink',
		'<a title="' . _('Revocation') .
		'" href="/index.php?page=page&title=page-revocation&name=' . _('Revocation') . '" target="_blank">' .
		_('Link to the Revocaton in full text') . '</a>');

	// Buttons
	$refPage->assign('formstart',
		'<form action = "' . URL . '?page=cart&title=send' .
		'&name=' . _('Shoppingcart') . '&cart=send" method = "post">');
	$refPage->assign('formend', '</form>');

	$refPage->assign('order_button',
		'<input class="btn_order" name="btn_order" title="' . _('Buy now') .
		'" src="template/images/btn_order_send.png" alt="' . _('Buy now') .
		'" type="image">');
	$refPage->assign('back_button',
		'<a title="' . _('Back') . '"href="/index.php?page=cart&title=order&name= Warenkorb&cart=order">' .
		'<img src="/template/images/btn_back.png" alt = "' .
		_('Back') . '" titel = "Warenkorb"></a>');
	pagepointer::setCurrentPage();
	$refPage->addtpl('content', '/' . $page . '/confirm.tpl.php');
}
else
{
	header('location: index.php');
	exit;
}
?>